package mammuthus.web.filter;


import com.webdm.common.cookie.CookiesInfo;
import mammuthus.web.util.AuthUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import scala.Tuple2;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登陆信息设置
 * @author zhoufang
 *
 */
public class LoginFilter implements Filter {
	
	
    	private static Logger log= LogManager.getLogger(LoginFilter.class);
	//private UserService userService;


	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
	    
		HttpServletRequest httpRequest=(HttpServletRequest) request;
		HttpServletResponse httpResponse=(HttpServletResponse)response;
		

		String uri=httpRequest.getRequestURI();
		if(uri != null && (uri.endsWith("/l.html"))){
			chain.doFilter(request, response);
			return;
		}


		try {
		    String key = CookiesInfo.getValueFromCookies(httpRequest, AuthUtil.cookieKey());
            Tuple2<Object, String> auth = AuthUtil.parseKey(key);
            boolean flag = (Boolean)auth._1();
            if(flag){
                request.setAttribute("userName", auth._2());
                chain.doFilter(request, response);
            }else{
                httpResponse.sendRedirect("/l.html");
            }
		} catch (Exception e) {
		    // TODO Auto-generated catch block
		    e.printStackTrace();
		}


//        String reqUrl = httpRequest.getRequestURL().toString();
//		if(name == null || name.length() <= 5){
//
//		    	String url = "https://passport.csdn.net/account/login?from=http://catchs-conf.csdn.net/confList.html";
//		    	log.info("user name is err [" + name + "], url " + url);
//		    	httpResponse.sendRedirect(url);
//			return;
//		}else{
//
//
//		   // log.info("user login, name [" + name + "]");
//		    String sessionKey = "_user_" + name;
//		    UserBean userBean = (UserBean)httpRequest.getSession().getAttribute(sessionKey);
//		    if(userBean == null){
//
//                //验证用户是否存在
//                log.info("user login " + name);
//                userBean = userService.findUserByUserId(name);
//                if(userBean == null){
//                        log.info("user login, 用户尚未激活 [" + name + "]");
//                    PrintWriter  writer = httpResponse.getWriter();
//                    writer.write("<html><center><h1>user not exists [" + name + "]</h1><center></html>");
//
//                    writer.flush();
//                    writer.close();
//
//                    return;
//                 }else{
//
//                    httpRequest.getSession().setAttribute(sessionKey, userBean);
//                 }
//
//		    }
//		    request.setAttribute("user", userBean);
//
//			chain.doFilter(request, response);
//
//		}
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
//		ApplicationContext applicationContext= WebApplicationContextUtils.getWebApplicationContext(filterConfig.getServletContext());
//		userService=(UserService) applicationContext.getBean("userService");
//
	}

	@Override
	public void destroy() {
	    
	    
	}

}
